package com.baymax.test.scene.internal.key

import android.view.KeyEvent
import com.baymax.lib.key.KeyProcessorConfig
import com.baymax.lib.key.KeySession
import com.baymax.lib.key.KeyTransformer

/**
 * 支持按数字键进入直播页
 */
abstract class WatchTvKeyTransFormer : KeyTransformer {


    override fun filter(keyEvent: KeyEvent): Boolean {
        return keyEvent.action == KeyEvent.ACTION_DOWN
    }

    override fun except(keyEvent: KeyEvent): Boolean {
        return keyEvent.keyCode >= KeyEvent.KEYCODE_0 && keyEvent.keyCode <= KeyEvent.KEYCODE_9
                || keyEvent.keyCode >= KeyEvent.KEYCODE_NUMPAD_0 && keyEvent.keyCode <= KeyEvent.KEYCODE_NUMPAD_9
    }

    override val config: KeyProcessorConfig = KeyProcessorConfig()

    override fun checkComplete(keySession: KeySession): List<KeyEvent>? {
        if (!keySession.isDisposed) {
            val sp = keySession.snapShot()
            if (sp.size >= 3) {
                val k =
                    sp.dropWhile { it.keyCode == KeyEvent.KEYCODE_0 || it.keyCode == KeyEvent.KEYCODE_NUMPAD_0 }
                if (k.size == 3) {
                    return k
                }
            }

        }
        return null
    }


}
